package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.admin.dtos.SensitiveDto;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.pojos.WmSensitive;
import com.heima.wemedia.mapper.WmSensitiveMapper;
import com.heima.wemedia.service.WmSensitiveService;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @Author: xrl
 * @Date: 2022/10/21  19:31
 * @Version 1.0
 */
@Service
public class WmSensitiveServiceimpl extends ServiceImpl<WmSensitiveMapper, WmSensitive> implements WmSensitiveService {

    /**
     * 查询敏感词列表
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult getList(SensitiveDto dto) {
        if (dto == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        //检查参数
        dto.checkParam();
        //分页查询
        IPage page = new Page(dto.getPage(), dto.getSize());

        //按照用户、时间查询
        LambdaQueryWrapper<WmSensitive> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByDesc(WmSensitive::getCreatedTime);
        if (!dto.getName().equals("")) {
            wrapper.eq(WmSensitive::getSensitives, dto.getName());
        }

        //调用方法查询
        page = page(page, wrapper);

        //返回结果
        ResponseResult result = new PageResponseResult(dto.getPage(), dto.getSize(), (int) page.getTotal());
        //为result 设置分页查询数据
        result.setData(page.getRecords());
        return result;
    }

    /**
     * 删除敏感词
     */
    @Override
    public ResponseResult deleteById(Integer id) {
        removeById(id);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 添加敏感词
     */
    @Override
    public ResponseResult saveSensitive(WmSensitive wmSensitive) {
        if (wmSensitive.getSensitives() == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }

        LambdaQueryWrapper<WmSensitive> wrapper = Wrappers.<WmSensitive>lambdaQuery()
                .eq(WmSensitive::getSensitives, wmSensitive.getSensitives());
        WmSensitive sensitive1 = getOne(wrapper);
        if (sensitive1 != null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST, "敏感词已存在");
        }
        //代表敏感词不存在
        wmSensitive.setCreatedTime(new Date());
        save(wmSensitive);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 修改敏感词
     */
    @Override
    public ResponseResult updateSensitive(WmSensitive wmSensitive) {
        if (wmSensitive == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        WmSensitive sensitive = null;
        if (wmSensitive.getId() != null) {
            sensitive = getById(wmSensitive.getId());
            sensitive.setSensitives(wmSensitive.getSensitives());
        }
        //修改
        updateById(sensitive);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
